package com.singlemountaintech.haogongjiang.http;

import android.content.Context;
import android.util.Log;

import com.framework.dialog.LoadingDialog;
import com.framework.dialog.ProgressCancelListener;
import com.singlemountaintech.haogongjiang.http.interceptor.ApiException;
import com.singlemountaintech.haogongjiang.utils.UiUtils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import rx.Subscriber;

public abstract class ProgressSubscriber<T> extends Subscriber<T> implements ProgressCancelListener {

    private static final String TAG = "ProgressSubscriber";
    private LoadingDialog mLoadingDialog;

    private Context context;
    private String mText;
    protected boolean showDialog = false;

    public ProgressSubscriber(Context context) {
        this(context, false);
    }

    public ProgressSubscriber(Context context, boolean show) {
        this(context, show, "");
    }

    public ProgressSubscriber(Context context, boolean show, String text) {
        this.context = context;
        this.showDialog = show;
        this.mText = text;
        mLoadingDialog = new LoadingDialog(context);
        mLoadingDialog.setProgressCancelListener(this);
    }

    private void showProgressDialog() {
        if (mLoadingDialog != null && showDialog) {
            if (mText != null) {
                mLoadingDialog.setText(mText);
            }
            mLoadingDialog.show();
        }

    }

    public void dismissProgressDialog() {
        if (mLoadingDialog != null && showDialog)
            mLoadingDialog.dismiss();
    }

    /**
     * 订阅开始时调用
     * 显示ProgressDialog
     */
    @Override
    public void onStart() {

        if (showDialog) {
            showProgressDialog();
        }
    }

    /**
     * 完成，隐藏ProgressDialog
     */
    @Override
    public void onCompleted() {
        dismissProgressDialog();
    }

    /**
     * 对错误进行统一处理
     * 隐藏ProgressDialog
     *
     * @param e
     */
    @Override
    public void onError(Throwable e) {
        dismissProgressDialog();
        if (e instanceof SocketTimeoutException) {
            UiUtils.showToast(null, "网络中断，请检查您的网络状态");
        } else if (e instanceof ConnectException) {
            UiUtils.showToast(null, "网络中断，请检查您的网络状态");
        } else if (e instanceof UnknownHostException) {
            UiUtils.showToast(null, "网络中断，请检查您的网络状态");
        } else if (e instanceof ApiException) {
            ApiException apiException = (ApiException) e;
            if (apiException.getErrorCode() == 403) {
                UiUtils.showToastNormal("权限不足");
            }
            Log.d("net_error", "onError: " + ((ApiException) e).getErrorCode() + e.getMessage());
        } else {
            Log.d("net_error", "error:" + e.getMessage());
        }
    }

    /**
     * 取消ProgressDialog的时候，取消对observable的订阅，同时也取消了http请求
     */
    @Override
    public void onCancelProgress() {
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }
}